package top.jle.spring.basic.dto;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import top.jle.jpa.basic.dao.PagedResult;
import top.jle.jpa.basic.entities.Entity;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public abstract class DTOConvertorJSON {

    public static <PO extends Entity,DTO extends AbstractDTO> PO dto2po(Class<PO> type, DTO dto){
        return JSONObject.parseObject(JSONObject.toJSONString(dto, JSONWriter.Feature.WriteEnumsUsingName),type);
    }
    public static  <PO extends Entity,DTO extends AbstractDTO> List<PO> dto2po(Class<PO> type,List<DTO> dtoList){
        return JSONArray.parseArray(JSONArray.toJSONString(dtoList, JSONWriter.Feature.WriteEnumsUsingName),type);
    }
    public static <PO extends Entity,DTO extends AbstractDTO> DTO po2dto(Class<DTO> type,PO po){

        return JSONObject.parseObject(JSONObject.toJSONString(po, JSONWriter.Feature.WriteEnumsUsingName),type);
    }
    public static <PO extends Entity,DTO extends AbstractDTO> List<DTO> po2dto(Class<DTO> type,List<PO> poList){
        return JSONArray.parseArray(JSONArray.toJSONString(poList, JSONWriter.Feature.WriteEnumsUsingName),type);
    }
    public static <PO extends Entity,DTO extends AbstractDTO> PagedResult<DTO> po2dto(Class<DTO> type, PagedResult<PO> pagedResult){
        PagedResult<DTO> result = new PagedResult<>(
                po2dto(type,pagedResult.getResultList()),
                pagedResult.getNextOffset(),
                pagedResult.getTotalCount(),
                pagedResult.getCurrentPage());
        result.setTotalPageNum(pagedResult.getTotalPageNum());
        return result;
    }

}

